<?php
/*同事表控制器：尽量运用基本请求方法a.GET和form.POST，结合用ajax请求，节省模板只用一个列表页，增加、编辑的模板代码放在列表页中，用JS调出来，在列表页用ajax分页，*/
class matesAction extends Action{
    const REF = 'index.php?act=mates&op=list';
    public function __construct(){
        Db::dbInstall();    //数据库连接
    }
    /**
     *同事列表，用ajax分页
     */
    public function listOp(){
        $list = mateModel::getList();
        include_once TEMPLATES_DIR.DS."mates_list.php";
    }
    public function addOp(){
        $insert_array['mates_name'] = $_POST['name'];
        $insert_array['mates_age'] = $_POST['age'];
        $insert_array['mates_job'] = $_POST['job'];
        $insert_array['mates_part'] = $_POST['part'];
        if(mateModel::addMate($insert_array)){
            $this->showMessage('添加成功',self::REF,1);
        }else{
            $this->showMessage('添加失败',self::REF);
        }
    }
    public function editOp(){
        $mates_id = $_POST['mates_id'];
        $array['mates_name'] = $_POST['name'];
        $array['mates_age'] = $_POST['age'];
        $array['mates_job'] = $_POST['job'];
        $array['mates_part'] = $_POST['part'];
        $where = "mates_id='$mates_id'";
        if(mateModel::updateMate($array,$where)){
            $this->showMessage('修改成功',self::REF,1);
        }else{
            $this->showMessage('修改失败或没有修改',self::REF);
        }
    }
    public function del_moreOp(){
        $mates_id_array = $_GET['del'];
        $mates_ids = implode(',',$mates_id_array);
        $sql = "delete from #__mates where mates_id in ($mates_ids)";
        if(Db::Delete($sql)){
            echo json_encode(array('state'=>true,'msg'=>'删除成功','ref'=>self::REF));
        }else{
            echo json_encode(array('state'=>false,'msg'=>'删除失败','ref'=>self::REF));
        }
    }

    /**
     *js事件调用编辑界面前，发送ajax请求，返回某个同事信息
     */
    public function ajax_get_mateOp(){
        $mates_id = $_GET['mates_id'];
        $sql = "select * from #__mates where mates_id='$mates_id'";
        $mates_row = Db::queryRow($sql);
        if($mates_row){
            $mates_row['state']=true;
            echo json_encode($mates_row);
        }else{
            echo json_encode(array('state'=>false));
        }
    }
}